package com.bayss.core.service.found;

import com.bayss.core.entity.found.ZaihuMessage;
import com.bayss.core.entity.found.ZaihuMessageComments;
import com.bayss.core.entity.found.ZaihuMessageCommentsExample;
import com.bayss.core.entity.found.mapper.ZaihuMessageCommentsMapper;
import com.bayss.core.entity.found.mapper.ZaihuMessageMapper;
import com.bayss.core.enums.MessageShow;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class MessageCommentsService {

    @Autowired
    ZaihuMessageCommentsMapper commentsMapper;

    @Autowired
    ZaihuMessageMapper messageMapper;


    /**
     * 添加留言的评论
     *
     * @param userId
     * @param comContent
     * @param messageId
     * @return
     */
    public ZaihuMessageComments addMessageComments(Integer userId, String comContent, Integer messageId) {
        ZaihuMessage messageEntity = messageMapper.selectByPrimaryKey(messageId);
        if (messageEntity != null) {
            ZaihuMessageComments messageCommentsEntity = new ZaihuMessageComments();
            messageCommentsEntity.setComContent(comContent);
            messageCommentsEntity.setComType(MessageShow.show);
            messageCommentsEntity.setCreateTime(new Date());
            messageCommentsEntity.setMessageId(messageId);
            messageCommentsEntity.setUserId(userId);
            messageCommentsEntity.setTitleName(messageEntity.getTitleName());
            commentsMapper.insertSelective(messageCommentsEntity);
            return messageCommentsEntity;
        }
        return null;
    }

    /**
     * 隐藏显示评论
     *
     * @param commentsId
     * @param comType
     */
    public void updateMessageCommentsType(Integer commentsId, Integer comType) {
        ZaihuMessageComments messageCommentsEntity = commentsMapper.selectByPrimaryKey(commentsId);
        messageCommentsEntity.setComType(comType);
        commentsMapper.updateByPrimaryKey(messageCommentsEntity);
        ZaihuMessage messageEntity = messageMapper.selectByPrimaryKey(messageCommentsEntity.getMessageId());
        if (messageEntity != null) {
            if (comType == MessageShow.show) {
                messageMapper.updateComentNumCream1(messageCommentsEntity.getMessageId());
            } else if (comType == MessageShow.hide) {
                messageMapper.updateComentNumdecrease1(messageCommentsEntity.getMessageId());
            }
        }
    }

    /**
     * 分页查询留言的评论
     */
    public PageInfo<ZaihuMessageComments> getMessageCommentsListByPage(Integer messageId, Integer status, Integer page, Integer pageSize) throws Exception {
        ZaihuMessageCommentsExample example = new ZaihuMessageCommentsExample();
        if (messageId != null) {
            example.createCriteria().andMessageIdEqualTo(messageId);
        }
        if (status != null) {
            example.createCriteria().andStatusEqualTo(status);
        }
        example.setOrderByClause("create_time desc");
        PageInfo<ZaihuMessageComments> result = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> commentsMapper.selectByExample(example));
        return result;
    }
}
